package com.weixin.xplore.dao.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.weixin.xplore.dao.common.BaseRepository;
import com.weixin.xplore.dao.entity.IntegralRecord;
import com.weixin.xplore.dao.entity.SaTokenDO;
import com.weixin.xplore.dao.service.SaTokenDaoService;
import lombok.RequiredArgsConstructor;
import org.anyline.adapter.KeyAdapter;
import org.anyline.data.param.ConfigStore;
import org.anyline.data.param.init.DefaultConfigStore;
import org.anyline.entity.DataRow;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;


@Service
@RequiredArgsConstructor
public class SaTokenDaoServiceImpl extends BaseRepository<SaTokenDO> implements SaTokenDaoService {

    @Override
    public SaTokenDO getBySaKey(String key) {
        ConfigStore configStore = new DefaultConfigStore();
        configStore.eq(getFieldName(SaTokenDO::getSaKey),key);
       return super.selectOne(configStore);
    }

    @Override
    public Boolean updateValueByKey(String value, String key) {
        SaTokenDO integralRecord  = new SaTokenDO();
        integralRecord.setSaValue(value);

        SaTokenDO bySaKey = getBySaKey(key);
        integralRecord.setId(bySaKey.getId());

        return super.updateById(integralRecord);
    }

    @Override
    public Boolean deleteByKey(String key) {
        SaTokenDO bySaKey = getBySaKey(key);
        return super.deleteById(bySaKey);
    }

    @Override
    public Boolean updateTimeoutByKey(long timeout, String key) {
        SaTokenDO integralRecord  = new SaTokenDO();
        integralRecord.setTimeout(timeout);

        SaTokenDO bySaKey = getBySaKey(key);
        integralRecord.setId(bySaKey.getId());
        return super.updateById(integralRecord);
    }

    @Override
    public List<SaTokenDO> queryValidKey() {
        ConfigStore configStore = new DefaultConfigStore();
        DateTime date = DateUtil.date();
        date = DateUtil.offsetHour(date,24);
        configStore.gt(getFieldName(SaTokenDO::getTimeout),date.getTime());

        return super.listByCondition(configStore);
    }

    @Override
    public List<SaTokenDO> queryBySearchKey(String keyword) {
        ConfigStore configStore = new DefaultConfigStore();
        configStore.eq(getFieldName(SaTokenDO::getSaKey),keyword);
        return super.listByCondition(configStore);
    }

    @Override
    public Boolean save(SaTokenDO saTokenDO) {
        return super.save(saTokenDO);
    }
}
